package org.dromara.Nyt7StatisticsClaimAmountRegion.service.impl;

import org.dromara.Nyt7Claim.domain.bo.Nyt7ClaimBo;
import org.dromara.Nyt7Claim.domain.vo.Nyt7ClaimVo;
import org.dromara.Nyt7Claim.service.INyt7ClaimService;
import org.dromara.Nyt7StatisticsClaimAmountRegion.domain.vo.Nyt7StatisticsClaimAmountRegionVo;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7StatisticsClaimAmountRegion.domain.bo.Nyt7StatisticsClaimAmountRegionBo;
import org.dromara.Nyt7StatisticsClaimAmountRegion.service.INyt7StatisticsClaimAmountRegionService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import static org.dromara.utils.utils.ClaimUtil.filter;

/**
 * 统计分析_投保统计_数量统计_按地区的数量统计Service业务层处理
 *
 * @author yt
 * @date 2025-01-06
 */
@RequiredArgsConstructor
@Service
public class Nyt7StatisticsClaimAmountRegionServiceImpl implements INyt7StatisticsClaimAmountRegionService {

    private final INyt7ClaimService nyt7claimService;

    /**
     * 分页查询统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 统计分析_投保统计_数量统计_按地区的数量统计分页列表
     */
    @Override
    public TableDataInfo<Nyt7StatisticsClaimAmountRegionVo> queryPageList(Nyt7StatisticsClaimAmountRegionBo bo, PageQuery pageQuery) {

        List<Nyt7StatisticsClaimAmountRegionVo> statisticsVos = getStatisticsVos(bo);
        Page<Object> build = pageQuery.build();
        // 创建 Page 对象
        Page<Nyt7StatisticsClaimAmountRegionVo> page = new Page<>(build.getCurrent(),build.getSize());
        page.setRecords(statisticsVos);
        page.setTotal(statisticsVos.size());
        return TableDataInfo.build(page);
    }

    private List<Nyt7StatisticsClaimAmountRegionVo> getStatisticsVos(Nyt7StatisticsClaimAmountRegionBo bo) {

        Nyt7ClaimBo nyt7ClaimBo = new Nyt7ClaimBo();
        nyt7ClaimBo.setParams(bo.getParams());
        nyt7ClaimBo.setRegion(bo.getRegion());
        List<Nyt7ClaimVo> nyt7ClaimVoList = nyt7claimService.queryList(nyt7ClaimBo);
        //获得总面积
        double sum = nyt7ClaimVoList.stream().mapToDouble(Nyt7ClaimVo::getClaimAmount).sum();


        List<Nyt7ClaimVo> nyt7ClaimVos = filter(nyt7ClaimVoList, "region");

        List<Nyt7StatisticsClaimAmountRegionVo> vos = new ArrayList<>();
        long i = 1;
        if(!nyt7ClaimVos.isEmpty()){
            for (Nyt7ClaimVo nyt7ClaimVo : nyt7ClaimVos){
                Nyt7StatisticsClaimAmountRegionVo vo = new Nyt7StatisticsClaimAmountRegionVo();
                vo.setId(i++);
                vo.setRegion(nyt7ClaimVo.getRegion());
                vo.setClaimAmount(BigDecimal.valueOf(nyt7ClaimVo.getClaimAmount()));
                vo.setAmountRatio(BigDecimal.valueOf(nyt7ClaimVo.getClaimAmount() / sum).setScale(4, RoundingMode.HALF_UP));
                vo.setCreateTime(nyt7ClaimVo.getCreateTime());
                vos.add(vo);
            }
        }

        return vos;
    }

    /**
     * 查询符合条件的统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo 查询条件
     * @return 统计分析_投保统计_数量统计_按地区的数量统计列表
     */
    @Override
    public List<Nyt7StatisticsClaimAmountRegionVo> queryList(Nyt7StatisticsClaimAmountRegionBo bo) {
        return getStatisticsVos(bo);
    }
}
